package com.ruoyi.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.crm.domain.CrmOrder;
import com.ruoyi.crm.domain.CrmProject;
import com.ruoyi.crm.domain.dto.CrmProjectDTO;
import com.ruoyi.crm.service.ICrmProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目Controller
 *
 * @author yanwenliang
 * @date 2024-08-16
 */
@RestController
@RequestMapping("/crm/project")
public class CrmProjectController extends BaseController
{
    @Autowired
    private ICrmProjectService crmProjectService;

    /**
     * 查询项目列表
     */
    @PreAuthorize("@ss.hasPermi('crm:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmProject crmProject) {
        startPage();
        List<CrmProjectDTO> list = crmProjectService.selectCrmProjectList(crmProject);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @PreAuthorize("@ss.hasPermi('crm:project:export')")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmProject crmProject)
    {
        List<CrmProjectDTO> crmProjectDTOList = crmProjectService.selectCrmProjectList(crmProject);
        List<CrmProject> list = new ArrayList();
        for (CrmProjectDTO crmProjectDTO : crmProjectDTOList) {
            CrmProject newCrmProject = new CrmProject();
            BeanUtils.copyBeanProp(crmProjectDTO, newCrmProject);
            list.add(newCrmProject);
        }
        ExcelUtil<CrmProject> util = new ExcelUtil<CrmProject>(CrmProject.class);
        util.exportExcel(response, list, "项目数据");
    }

    /**
     * 获取项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") String projectId)
    {
        return AjaxResult.success(crmProjectService.selectCrmProjectByProjectId(projectId));
    }

    /**
     * 新增项目
     */
    @PreAuthorize("@ss.hasPermi('crm:project:add')")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmProject crmProject)
    {
        return toAjax(crmProjectService.insertCrmProject(crmProject));
    }

    /**
     * 修改项目
     */
    @PreAuthorize("@ss.hasPermi('crm:project:edit')")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmProject crmProject)
    {
        return toAjax(crmProjectService.updateCrmProject(crmProject));
    }

    /**
     * 删除项目
     */
    @PreAuthorize("@ss.hasPermi('crm:project:remove')")
    @Log(title = "项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds)
    {
        return toAjax(crmProjectService.deleteCrmProjectByProjectIds(projectIds));
    }


    /**
     * 查询自己负责的项目
     * @param crmProject
     * @return
     */
    @GetMapping("/getCrmProjectListByUser")
    public TableDataInfo getCrmProjectListByUser(CrmProject crmProject){
        startPage();
        List<CrmProjectDTO> list = crmProjectService.getCrmProjectListByUser(crmProject);
        return getDataTable(list);
    }



    /**
     * 查询自己负责的项目
     * @param crmProject
     * @return
     */
    @PostMapping("/getProjectByStatus")
    public AjaxResult getProjectByStatus(@RequestBody CrmProject crmProject){
        CrmProject project = crmProjectService.getOne(new LambdaQueryWrapper<CrmProject>().eq(CrmProject::getStatus, crmProject.getStatus())
                .eq(CrmProject::getUserId, SecurityUtils.getUserId()));
        return AjaxResult.success(project);
    }

    /**
     * 获取项目信息
     * @param crmProject
     * @return
     */
    @PostMapping("getCrmProjectAllNoPage")
    public AjaxResult getCrmProjectAllNoPage(@RequestBody CrmProject crmProject){
        List<CrmProjectDTO> list = crmProjectService.selectCrmProjectList(crmProject);
        return AjaxResult.success(list);
    }
}
